package com.headStorm.Joystick;

/**
 * @author zhangqq
 * @create 2021-10-27 14:01
 **/
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;

class SimpleJoystick_Error extends Error {
    public SimpleJoystick_Error (String message) {
        super(message);
    }
}

class SimpleJoystick_Connection_Error extends SimpleJoystick_Error {
    public SimpleJoystick_Connection_Error (String message) {
        super(message);
    }
}

public class SimpleJoystick {
    private Controller joystick;
    private Component xAxis, yAxis, zAxis, slider, pov;

    public SimpleJoystick(String name) {
        Controller[] devices = ControllerEnvironment.getDefaultEnvironment().getControllers();

        for(int i = 0; i < devices.length && joystick == null; i++) {
            if(devices[i].getName().equals(name)) {
                joystick = devices[i];
            }
        }

        if(joystick == null) {
            throw new SimpleJoystick_Connection_Error("No connected device is called '" + name + "'.");
        } else {
            Component[] controls = joystick.getComponents();
            for (Component control : controls) {
                if (control.getIdentifier() == Component.Identifier.Axis.X) {
                    xAxis = control;
                }
                if (control.getIdentifier() == Component.Identifier.Axis.Y) {
                    yAxis = control;
                }
                if (control.getIdentifier() == Component.Identifier.Axis.Z) {
                    zAxis = control;
                }
                if (control.getIdentifier() == Component.Identifier.Axis.SLIDER) {
                    slider = control;
                }
                if (control.getIdentifier() == Component.Identifier.Axis.POV) {
                    pov = control;
                }
            }
        }
    }

    public void poll() {
        if(!joystick.poll()) {
            throw new SimpleJoystick_Connection_Error("The joystick appears to have been disconnected.");
        }
    }

    public float pov() {
        if(pov == null) {
            throw new Error("The joystick doesn't appear to have a POV.");
        } else {
            return pov.getPollData();
        }
    }

    public float X() {
        if(xAxis == null) {
            throw new Error("The joystick doesn't appear to have an x-axis.");
        } else {
            return xAxis.getPollData() * -1;
        }
    }

    public float Y() {
        if(yAxis == null) {
            throw new Error("The joystick doesn't appear to have an y-axis.");
        } else {
            return yAxis.getPollData() * -1;
        }
    }

    public float Z() {
        if(zAxis == null) {
            throw new Error("The joystick doesn't appear to have an z-axis.");
        } else {
            return zAxis.getPollData() * -1;
        }
    }

    public float slider() {
        if(slider == null) {
            throw new Error("The joystick doesn't appear to have a slider.");
        } else {
            return slider.getPollData() * -1;
        }
    }


    public static void listAllControllers(){
        ControllerEnvironment defaultEnvironment = ControllerEnvironment.getDefaultEnvironment();

        Controller[] controllers = defaultEnvironment.getControllers();

        while (true){
            for (Controller controller : controllers) {
                Component[] components = controller.getComponents();
                if( "Stick".equals(controller.getType().toString())){
                    System.out.println(("Controller :" + controller.getName() + " type:" + controller.getType().toString()));
                    int i = 0;
                    for (Component component : components) {
                        if(component.getIdentifier() instanceof Component.Identifier.Button){
                            System.out.println("button" + (i++));
                            float diff = component.getPollData();
                            System.out.println(diff);
                            if(diff > 0.5){
                                System.err.println("aaaaaaaaaaa");
                            }
                        }
                    }
                }
        }


            //System.out.println("Component Count: "+components.length);
        }
      
          //for(int i =0;i<ca.length;i++){


              // Get this controllers components (buttons and axis)
//              for(int j=0;j<components.length;j++){
//                  System.out.println("\t"+j+": "+components[j].getName() + " " + components[j].getIdentifier().getName()
//                                        +" ("
//                        + (components[j].isRelative() ? "Relative" : "Absolute")
//                        + " "
//                        + (components[j].isAnalog()? "Analog" : "Digital")
//                        + ")");
//              }
    }


    public static  void detectSingleInputChangeB() {
        int i, j;
        while (true) {
            // get the latest state
            // Inputma.update(true);
            Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
            for (i = 0; i < ca.length; i++) {
                Component[] components = ca[i].getComponents();
                for (j = 0; j < components.length; j++) {
                    float diff = components[j].getPollData();
                    if (diff > 0.5) {
                        // change to state
                        System.out.println("Found " + ca[i].getName() + "." + components[j].getName() + "=" + diff);
                        //return;
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        listAllControllers();
    }



}
